package org.budo.jdbc.driver;

import java.sql.SQLException;
import java.sql.Types;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import lombok.extern.slf4j.Slf4j;

/**
 * @author lmw
 */
@Slf4j
@Getter
@Setter
@Accessors(fluent = true)
@NoArgsConstructor
public class BudoJdbcResultSetMetaData extends AbstractBudoJdbcResultSetMetaData {
    private String[] columnNames;

    private String[] columnLabels;

    private int[] columnTypes;

    private String[] columnClassNames;

    public BudoJdbcResultSetMetaData(String[] columnNames) {
        this.columnNames(columnNames);
    }

    public BudoJdbcResultSetMetaData(String[] columnNames, int columnType, Class<?> columnClass) {
        String[] columnLabels = new String[columnNames.length];
        int[] columnTypes = new int[columnNames.length];
        String[] columnClassNames = new String[columnNames.length];

        for (int i = 0; i < columnNames.length; i++) {
            columnLabels[i] = columnNames[i];
            columnTypes[i] = columnType;
            columnClassNames[i] = columnClass.getName();
        }

        this.columnNames(columnNames);
        this.columnLabels(columnLabels);
        this.columnTypes(columnTypes);
        this.columnClassNames(columnClassNames);
    }

    public int getColumnCount() throws SQLException {
        return this.columnNames().length;
    }

    public String getColumnName(int column) throws SQLException {
        return this.columnNames()[column - 1];
    }

    @Override
    public String getColumnLabel(int column) throws SQLException {
        if (null == this.columnLabels()) {
            return this.getColumnName(column);
        }

        return this.columnLabels()[column - 1];
    }

    @Override
    public int getColumnType(int column) throws SQLException {
        if (null == this.columnTypes()) {
            return Types.VARCHAR;
        }

        return this.columnTypes()[column - 1];
    }

    @Override
    public String getColumnClassName(int column) throws SQLException {
        if (null == this.columnClassNames()) {
            return Object.class.getName();
        }

        return this.columnClassNames()[column - 1];
    }

    @Override
    public String getSchemaName(int column) throws SQLException {
        log.info("#27 getSchemaName return \"\"");
        return "";
    }

    @Override
    public String getTableName(int column) throws SQLException {
        return "";
    }

    @Override
    public int getColumnDisplaySize(int column) throws SQLException {
        return Integer.MAX_VALUE;
    }

    /**
     * 小数点右边的位数
     */
    @Override
    public int getScale(int column) throws SQLException {
        return 0;
    }

    /**
     * 精度,有效位
     */
    @Override
    public int getPrecision(int column) throws SQLException {
        return 0;
    }

    /**
     * 是否有符号,区分正负
     */
    @Override
    public boolean isSigned(int column) throws SQLException {
        return true;
    }
}
